package com.nl.pulsardemo.pool.service.impl;

import com.nl.pulsardemo.pool.config.commons.utils.ResultVOUtils;
import com.nl.pulsardemo.pool.service.IProduce;
import com.nl.pulsardemo.pool.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClientException;
import org.springframework.stereotype.Service;

/**
 * @author : ZhaoY
 * @description :
 * @date : 2021-02-01 16:02
 **/
@Service
@Slf4j
public class ProduceImpl implements IProduce {
    @Override
    public ResultVO SyncSend(Producer producer, String param) {
        try {
            producer.send(param);//同步发送
            log.info("SyncSend  message: " + param);
        } catch (PulsarClientException e) {
            return ResultVOUtils.error(-1, "生产失败");
        }
        return ResultVOUtils.success("生产成功");
    }

    @Override
    public ResultVO AsyncSend(Producer producer, String param) {
        producer.sendAsync(param).thenAccept(msgId -> {
            log.info("msgId:" + msgId + ",AsyncSend message: " + param);
        });
        return ResultVOUtils.success("生产成功");
    }
}
